#include "excelUtils.h"


void ExcelUtils::saveExcel(QList<DaoModel::TestData>& dataList, QString fileName) {
	//创建Excel
	QXlsx::Document document;
	//设置数据单元格样式
	QXlsx::Format format;
	format.setFontSize(14);//字体大小
	format.setFont(QFont(QString("宋体")));
	format.setVerticalAlignment(QXlsx::Format::AlignVCenter);//垂直居中
	format.setHorizontalAlignment(QXlsx::Format::AlignLeft);//水平居中
	//检测信息
	document.mergeCells("A1:L1", format);
	document.mergeCells("A2:F2", format);
	document.mergeCells("G2:L2", format);
	document.write("A1", QString("检测时间: %1").arg(dataList[0].createTime().toString("yyyy-MM-dd HH:mm:ss")), format);
	document.write("A2", QString("检测员: %1").arg(dataList[0].tester()), format);
	document.write("G2", QString("班次: %1").arg(dataList[0].schedule()), format);
	//============== 创建表头 ===================
	format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);//水平居中
	format.setVerticalAlignment(QXlsx::Format::AlignVCenter);//垂直居中
	format.setFontBold(true);//字体加粗
	format.setTextWarp(true);//自动换行
	//设置表头内容
	document.write(QString("A3"), QString("序号"), format);
	document.write(QString("B3"), QString("牌号"), format);
	document.write(QString("C3"), QString("编号"), format);
	document.write(QString("D3"), QString("薄膜最高点(mm)"), format);
	document.write(QString("E3"), QString("薄膜最低点(mm)"), format);
	document.write(QString("F3"), QString("薄膜高度平均值(mm)"), format);
	document.write(QString("G3"), QString("标准方差"), format);
	document.write(QString("H3"), QString("异常区域(mm^2)"), format);
	//==================== 填充数据 =====================
	format.setFontBold(false);//字体加粗
	for (int i = 0; i < dataList.size(); i++) {
		int row = i + 4;
		//序号
		document.write(row, 1, (i + 1), format);
		//牌号
		document.write(row, 2, dataList[i].brand(), format);
		//编号
		document.write(row, 3, dataList[i].num(), format);
		//薄膜最高点
		document.write(row, 4, QString::number(dataList[i].maxHeight(), 'f', Configuration::getInstance().accuracy.area), format);
		//薄膜最低点
		document.write(row, 5, QString::number(dataList[i].minHeight(), 'f', Configuration::getInstance().accuracy.area), format);
		//高度平均值
		document.write(row, 6, dataList[i].meanHeight(), format);
		//薄膜高度数据标准差
		document.write(row, 7, dataList[i].heightSD(), format);
		//薄膜异常区域
		document.write(row, 8, dataList[i].abnormalArea(), format);
	}
	document.saveAs(fileName);
}
